/*
模糊查询数据-----------------------------------------------------------------------------------------------
*
*
*/

/**
 * 模糊查询包含此字段的所有数据
 * @param {database} database -数据库对象
 * @param {string} datasheet -数据表
 * @param {string} likefield -模糊匹配对应字段名字
 * @param {string} likeincludedata -模糊查询匹配值
 * @returns {res} 查询结果
 */
const datalikeinclude = (database,datasheet,likefield,likeincludedata) => {
    return new Promise((resolve, reject) => {
        const sql = `select * from ${datasheet} where ${likefield} like '%${likeincludedata}%'`;
        database.query(sql, (err, res) => {
            if (err) {
                console.log(err.message);
                reject(err);
            } else {
                // console.log(res);
                resolve(res);
            }
        });
    });
}


/**
 * 模糊查询包含此字段开头的所有数据
 * @param {database} database -数据库对象
 * @param {string} datasheet -数据表
 * @param {string} likefield -模糊匹配对应字段名字
 * @param {string} likeincludedata -模糊查询匹配值
 * @returns {res} 查询结果
 */
const datalikebegin = (database,datasheet,likefield,likeincludedata) => {
    return new Promise((resolve, reject) => {
        const sql = `select * from ${datasheet} where ${likefield} like '%${likeincludedata}__%'`;
        database.query(sql, (err, res) => {
            if (err) {
                console.log(err.message);
                reject(err);
            } else {
                // console.log(res);
                resolve(res);
            }
        });
    });
}

module.exports = {
    datalikeinclude,
    datalikebegin
}